package com.heima.article.service.Impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.search.vos.SearchArticleVo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
@Transactional
public class ArticleFreemarkerServiceImpl implements ArticleFreemarkerService {

    //minio
    @Autowired
    private FileStorageService fileStorageService;
    //freemaker
    @Autowired
    private Configuration configuration;
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 生成静态文件上传到minIO中
     *
     * @param apArticle
     * @param content
     */
    @Override
    @Async//异步注解
    public void buildArticleToMinIO(ApArticle apArticle, String content) {
        if (content != null) {
            try {
                //指定模板文件
                Template template = configuration.getTemplate("article.ftl");
                //准备数据 Map
                Map contentDate = new HashMap<>();
                contentDate.put("content", JSON.parseArray(content));
                //准备输出流：   使用：  StringWriter  将数据保存在内存中
                StringWriter writer = new StringWriter();
                //合成 html 文件： 1: 数据参数   2：输出流：
                template.process(contentDate, writer);

                //上传至minio

                //创建输入流：
                ByteArrayInputStream is = new ByteArrayInputStream(writer.toString().getBytes());
                String path = fileStorageService.uploadHtmlFile("", apArticle.getId() + "html", is);

                //保存数据：
                apArticleMapper.update(apArticle, Wrappers.<ApArticle>lambdaUpdate()
                        .eq(ApArticle::getId, apArticle.getId())
                        .set(ApArticle::getStaticUrl, path));

                //发送Kafka信息
//                SearchArticleVo vo = new SearchArticleVo();
//                BeanUtils.copyProperties(apArticle,vo);
//                vo.setContent(content);
//                vo.setStaticUrl(path);
//                kafkaTemplate.send(ArticleConstants.ARTICLE_ES_SYNC_TOPIC,JSON.toJSONString(vo));

                SearchArticleVo vo = new SearchArticleVo();
                BeanUtils.copyProperties(apArticle,vo);
                vo.setContent(content);
                vo.setStaticUrl(path);

                kafkaTemplate.send(ArticleConstants.ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(vo));
            } catch (Exception e) {
                throw new RuntimeException("生成静态页面失败");
            }
        }

    }

}
